package com.lovo.maotai.controller;

import com.alibaba.fastjson2.JSONObject;
import com.lovo.maotai.dao.IStaticPageDao;
import com.lovo.maotai.entity.StaticPageEntity;
import com.lovo.util.Verifier;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.TimeUnit;

@RequestMapping("maotai")
@RestController
public class StaticPageController {
    @Autowired
    IStaticPageDao staticPageDao;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    RedisTemplate redisTemplate;
    //加载静态页面
    @GetMapping("staticPage")
    public String staticPage(HttpServletRequest request){
    if(Verifier.verifierJwt(request.getHeader("token")).getTag()==0){
      return "";
    }
     StaticPageEntity spg= staticPageDao.findDistinctByPageTitle("mt");

        return spg.getPageInfo();
    }

   //抢购放入MQ
    @PostMapping("buy")
    public boolean buy(@RequestBody String token){

        rabbitTemplate.convertAndSend("directExchangeMT","mt",JSONObject.parseObject(token).get("token"));
        return true;
    }

    //获取结果
    @GetMapping("result")
    public boolean result(HttpServletRequest request){
      String token=  request.getHeader("token");
      Object obj=  redisTemplate.opsForList().leftPop("mtResult",5, TimeUnit.SECONDS);
       if(null!=obj){
           return true;
       }
        return false;
    }
}
